#!/bin/bash
# 阿里云ECS WebSocket服务器部署脚本

echo "开始部署WebSocket服务器到阿里云ECS..."

# 更新系统
apt update && apt upgrade -y

# 安装Python3和pip
apt install -y python3 python3-pip

# 创建项目目录
mkdir -p /root/websocket_server
cd /root/websocket_server

# 上传文件（需要手动上传）
# scp websocket_server.py root@your-ecs-ip:/root/websocket_server/
# scp requirements.txt root@your-ecs-ip:/root/websocket_server/

# 安装依赖
pip3 install -r requirements.txt

# 创建systemd服务
cat > /etc/systemd/system/patient-websocket.service << 'EOF'
[Unit]
Description=Patient Management WebSocket Server
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root/websocket_server
ExecStart=/usr/bin/python3 /root/websocket_server/websocket_server.py
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
EOF

# 重新加载systemd
systemctl daemon-reload

# 启用服务
systemctl enable patient-websocket.service

# 启动服务
systemctl start patient-websocket.service

# 检查服务状态
systemctl status patient-websocket.service

echo "部署完成！"
echo "服务状态: systemctl status patient-websocket.service"
echo "查看日志: journalctl -u patient-websocket.service -f"
echo "重启服务: systemctl restart patient-websocket.service"
